package com.whut.whw.util;

import android.graphics.Bitmap;

/**
 * Created by 张芳容 on 2017/06/19.
 */

public class ImageViewUtils {
    /**
     * 获取正确缩放裁剪适应IamgeView的Bitmap
     * @param widthTarget
     * @param heightTarget
     * @param bitmap
     * @return
     */
    public static Bitmap createFitBitmap(int widthTarget,int heightTarget, Bitmap bitmap) {
       // int widthTarget = imageView.getWidth();
        //int heightTarget = imageView.getHeight();
        int widthBitmap = bitmap.getWidth();
        int heightBitmap = bitmap.getHeight();
        LogUtils.e("剪裁图片", "widthTarget = " + widthTarget );
        LogUtils.e("剪裁图片", "heightTarget = " + heightTarget );
        LogUtils.e("剪裁图片", "widthBitmap = " + widthBitmap );
        LogUtils.e("剪裁图片", "heightBitmap = " + heightBitmap );
        Bitmap result = null;
        if( widthBitmap >= widthTarget && heightBitmap >= heightTarget ){
            result = createLargeToSmallBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
        }
        else if( widthBitmap >= widthTarget && heightBitmap < heightTarget ){
            Bitmap temp = createLargeWidthToEqualHeightBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
            result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);
        }
        else if( widthBitmap < widthTarget && heightBitmap >= heightTarget ){
            Bitmap temp = createLargeHeightToEqualWidthBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
            result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);
        }
        else{
            Bitmap temp = createSmallToEqualBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
            result = createFitBitmap(widthTarget,heightTarget, temp);
        }
        LogUtils.e("剪裁图片", "createFitBitmap--------------------->");
        return result;
    }



    private static Bitmap createLargeToSmallBitmap( int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){
        LogUtils.e("剪裁图片", "createLargeToSmallBitmap<---------------------");
        LogUtils.e("剪裁图片", "widthTarget = " + widthTarget );
        LogUtils.e("剪裁图片", "heightTarget = " + heightTarget );
        LogUtils.e("剪裁图片", "widthBitmap = " + widthBitmap );
        LogUtils.e("剪裁图片", "heightBitmap = " + heightBitmap );
        int x = (widthBitmap - widthTarget) / 2;
        int y = (heightBitmap - heightTarget) / 2;
        LogUtils.e("剪裁图片", "createLargeToSmallBitmap--------------------->");
        return Bitmap.createBitmap(bitmap, x, y, widthTarget, heightTarget);
    }

    private static Bitmap createLargeWidthToEqualHeightBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){

        LogUtils.e("剪裁图片", "createLargeWidthToEqualHeightBitmap<---------------------");
        LogUtils.e("剪裁图片", "widthTarget = " + widthTarget );
        LogUtils.e("剪裁图片", "heightTarget = " + heightTarget );
        LogUtils.e("剪裁图片", "widthBitmap = " + widthBitmap );
        LogUtils.e("剪裁图片", "heightBitmap = " + heightBitmap );
        double scale = ( heightTarget * 1.0 ) / heightBitmap;
        LogUtils.e("剪裁图片", "createLargeWidthToEqualHeightBitmap--------------------->");
        return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale) , heightTarget, false);
    }

    private static Bitmap createLargeHeightToEqualWidthBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){

        LogUtils.e("剪裁图片", "createLargeHeightToEqualWidthBitmap<---------------------");
        LogUtils.e("剪裁图片", "widthTarget = " + widthTarget );
        LogUtils.e("剪裁图片", "heightTarget = " + heightTarget );
        LogUtils.e("剪裁图片", "widthBitmap = " + widthBitmap );
        LogUtils.e("剪裁图片", "heightBitmap = " + heightBitmap );
        double scale = ( widthTarget * 1.0 ) / widthBitmap;
        LogUtils.e("剪裁图片", "createLargeHeightToEqualWidthBitmap--------------------->");
        return Bitmap.createScaledBitmap(bitmap, widthTarget , (int)(heightTarget * scale), false);
    }

    private static Bitmap createSmallToEqualBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){

        LogUtils.e("剪裁图片", "createSmallToEqualBitmap<---------------------");
        LogUtils.e("剪裁图片", "widthTarget = " + widthTarget );
        LogUtils.e("剪裁图片", "heightTarget = " + heightTarget );
        LogUtils.e("剪裁图片", "widthBitmap = " + widthBitmap );
        LogUtils.e("剪裁图片", "heightBitmap = " + heightBitmap );
        double scaleWidth = ( widthTarget * 1.0 ) / widthBitmap;
        double scaleHeight = ( heightTarget * 1.0 ) / heightBitmap;
        double scale = Math.min(scaleWidth, scaleHeight);
        LogUtils.e("剪裁图片", "createSmallToEqualBitmap--------------------->");
        return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale), (int)(heightBitmap * scale), false);
    }
}
